import axios from 'axios';
import Cookies from 'js-cookie';
const apiMethods = {
  methods: {
    apiGet(url, data) {
      return new Promise((resolve, reject) => {
        axios.get(url, data).then((response) => {
          resolve(response.data)
        }, (response) => {
          reject(response)
          this.Message('请求超时，请检查网络');
        })
      })
    },
    apiPost(url, data) {
      return new Promise((resolve, reject) => {
        axios.post(url, data).then((response) => {
          resolve(response.data)
        }).catch((response) => {
          console.log('f', response)
          resolve(response)
          this.Message('请求超时，请检查网络');
        })
      })
    },
    apiDelete(url, id) {
      return new Promise((resolve, reject) => {
        axios.delete(url + id).then((response) => {
          resolve(response.data)
        }, (response) => {
          reject(response)
          this.Message('请求超时，请检查网络');
        })
      })
    },
    apiPut(url, id, obj) {
      return new Promise((resolve, reject) => {
        axios.put(url + id, obj).then((response) => {
          resolve(response.data)
        }, (response) => {
          this.Message('请求超时，请检查网络');
          reject(response)
        })
      })
    },
    handelResponse(res, cb, errCb) {
      if (res.code == 200) {
        cb(res.data)
      } else {
        if (typeof errCb == 'function') {
          errCb()
        }
        this.handleError(res)
      }
    },
    handleError(res) {
      if (res.code) {
        switch (res.code) {
          case 101:
            console.log('cookie = ', Cookies.get('rememberPwd'))
            if (Cookies.get('rememberPwd')) {
              
              let data = {
                rememberKey: Cookies.get('rememberKey')
              }
              this.reAjax('admin/base/relogin', data).then((res) => {
                this.handelResponse(res, (data) => {
                  this.resetCommonData(data)
                })
              })
            } else {
              console.log(Cookies.get());
              this.Message(res.error);
              setTimeout(() => {
                this.$router.push({name: 'login'}) 
              }, 1500)
            }
            break
          case 103:
            this.Message(res.error);
            setTimeout(() => {
              this.$router.push({name: 'home_index'})
            }, 1500)
            break
          default :
            this.Message(res.error);
        }
      } else {
        console.log('default error')
      }
    },
    resetCommonData(data) {
      _(data.menusList).forEach((res, key) => {
        if (key == 0) {
          res.selected = true
        } else {
          res.selected = false
        }
      })
      Cookies.set('menus', data.menusList)              // 菜单数据
      Cookies.set('authKey', data.authKey)              // 权限认证
      Cookies.set('rememberKey', data.rememberKey)      // 记住密码的加密字符串
      Cookies.set('authList', data.authList)            // 权限节点列表
      Cookies.set('userInfo', data.userInfo)            // 用户信息
      Cookies.set('sessionId', data.sessionId)          // 用户sessionid
      window.axios.defaults.headers.authKey = Cookies.get('authKey')
      let routerUrl = ''
      if (data.menusList[0].url) {
        routerUrl = data.menusList[0].url
      } else {
        routerUrl = data.menusList[0].child[0].child[0].url
      }
      setTimeout(() => {
        let path = this.$route.path
        if (routerUrl != path) {
          router.replace(routerUrl)
        } else {
          this.$router.push({name: 'home_index'})
          
        }
      }, 1000)
    },
    reAjax(url, data) {
      return new Promise((resolve, reject) => {
        axios.post(url, data).then((response) => {
          resolve(response.data)
        }, (response) => {
          reject(response)
          this.Message('请求超时，请检查网络');
        })
      })
    },
    Message(msg){
        this.$Message.warning({
          content: msg,
          duration: 10,
          closable: true
        });
    }
  },
  computed: {
    showLoading() {
      return store.state.globalLoading
    }
  }
}

export default apiMethods
